home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / MPEG / Layer2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  10.5 KB  |  348 lines

  1. /* 
  2.  *  Layer2.cpp
  3.  *
  4.  *  Code from
  5.  *            NekoAmp 1.3 decoder by Avery Lee
  6.  *
  7.  *  FlasKMPEG
  8.  *    Copyright (C) Alberto Vigata - January 2000
  9.  *
  10.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  11.  *    
  12.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  13.  *  it under the terms of the GNU General Public License as published by
  14.  *  the Free Software Foundation; either version 2, or (at your option)
  15.  *  any later version.
  16.  *   
  17.  *  FlasKMPEG is distributed in the hope that it will be useful,
  18.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *  GNU General Public License for more details.
  21.  *   
  22.  *  You should have received a copy of the GNU General Public License
  23.  *  along with GNU Make; see the file COPYING.  If not, write to
  24.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  25.  *
  26.  */
  27.  
  28. #include <crtdbg.h>
  29. #include <string.h>
  30.  
  31. #include <stdio.h>
  32.  
  33. #include "AMPDecoder.h"
  34.  
  35. // combined 'c' table and (2/(steps+1))
  36.  
  37. static const double c_f_tab[17] = {
  38.  
  39. #define F(c,steps) ( 32768.0 * (c) * 2.0 / ((steps)+1) )
  40.  
  41.     F(1.33333333333,3),
  42.     F(1.60000000000,7),
  43.     F(1.14285714286,7),
  44.     F(1.77777777777,15),
  45.     F(1.06666666666,15),
  46.     F(1.03225806452,31),
  47.     F(1.01587301587,63),
  48.     F(1.00787401575,127),
  49.     F(1.00392156863,255),
  50.     F(1.00195694716,511),
  51.     F(1.00097751711,1023),
  52.     F(1.00048851979,2047),
  53.     F(1.00024420024,4095),
  54.     F(1.00012208522,8191),
  55.     F(1.00006103888,16383),
  56.     F(1.00003051851,32767),
  57.     F(1.00001525902,65535),
  58.  
  59. #undef F
  60.  
  61. };
  62.  
  63. static const double scalefactors[64] = {
  64. 2.00000000000000, 1.58740105196820, 1.25992104989487,
  65. 1.00000000000000, 0.79370052598410, 0.62996052494744, 0.50000000000000,
  66. 0.39685026299205, 0.31498026247372, 0.25000000000000, 0.19842513149602,
  67. 0.15749013123686, 0.12500000000000, 0.09921256574801, 0.07874506561843,
  68. 0.06250000000000, 0.04960628287401, 0.03937253280921, 0.03125000000000,
  69. 0.02480314143700, 0.01968626640461, 0.01562500000000, 0.01240157071850,
  70. 0.00984313320230, 0.00781250000000, 0.00620078535925, 0.00492156660115,
  71. 0.00390625000000, 0.00310039267963, 0.00246078330058, 0.00195312500000,
  72. 0.00155019633981, 0.00123039165029, 0.00097656250000, 0.00077509816991,
  73. 0.00061519582514, 0.00048828125000, 0.00038754908495, 0.00030759791257,
  74. 0.00024414062500, 0.00019377454248, 0.00015379895629, 0.00012207031250,
  75. 0.00009688727124, 0.00007689947814, 0.00006103515625, 0.00004844363562,
  76. 0.00003844973907, 0.00003051757813, 0.00002422181781, 0.00001922486954,
  77. 0.00001525878906, 0.00001211090890, 0.00000961243477, 0.00000762939453,
  78. 0.00000605545445, 0.00000480621738, 0.00000381469727, 0.00000302772723,
  79. 0.00000240310869, 0.00000190734863, 0.00000151386361, 0.00000120155435,
  80. 1E-20
  81. };
  82.  
  83.  
  84. /* ABCD_INDEX = lookqt[mode][sr_index][br_index]  */
  85. /* -1 = invalid  */
  86. static signed char lookqt[4][3][16] =
  87. {
  88.    1, -1, -1, -1, 2, -1, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1,        /*  44ks stereo */
  89.    0, -1, -1, -1, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1,        /*  48ks */
  90.    1, -1, -1, -1, 3, -1, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1,        /*  32ks */
  91.    1, -1, -1, -1, 2, -1, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1,        /*  44ks joint stereo */
  92.    0, -1, -1, -1, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1,        /*  48ks */
  93.    1, -1, -1, -1, 3, -1, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1,        /*  32ks */
  94.    1, -1, -1, -1, 2, -1, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1,        /*  44ks dual chan */
  95.    0, -1, -1, -1, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1,        /*  48ks */
  96.    1, -1, -1, -1, 3, -1, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1,        /*  32ks */
  97. // mono extended beyond legal br index
  98. //  1,2,2,0,0,0,1,1,1,1,1,1,1,1,1,-1,          /*  44ks single chan */
  99. //  0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,-1,          /*  48ks */
  100. //  1,3,3,0,0,0,1,1,1,1,1,1,1,1,1,-1,          /*  32ks */
  101. // legal mono
  102.    1, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,        /*  44ks single chan */
  103.    0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1,        /*  48ks */
  104.    1, 3, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,        /*  32ks */
  105. };
  106.  
  107. /* bit allocation table look up */
  108. /* table per mpeg spec tables 3b2a/b/c/d  /e is mpeg2 */
  109. /* look_bat[abcd_index][4][16]  */
  110. static const unsigned char look_bat[5][4][16] =
  111. {
  112. /* LOOK_BATA */
  113.    0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  114.    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17,
  115.    0, 1, 2, 3, 4, 5, 6, 17, 0, 0, 0, 0, 0, 0, 0, 0,
  116.    0, 1, 2, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  117. /* LOOK_BATB */
  118.    0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  119.    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17,
  120.    0, 1, 2, 3, 4, 5, 6, 17, 0, 0, 0, 0, 0, 0, 0, 0,
  121.    0, 1, 2, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  122. /* LOOK_BATC */
  123.    0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  124.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  125.    0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0,
  126.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  127. /* LOOK_BATD */
  128.    0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  129.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  130.    0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0,
  131.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  132. /* LOOK_BATE */
  133.    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  134.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  135.    0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0,
  136.    0, 1, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  137. };
  138.  
  139. static const unsigned char look_nbat[5][4] =
  140. {
  141.    3, 8, 12, 4,
  142.    3, 8, 12, 7,
  143.    2, 0, 6, 0,
  144.    2, 0, 10, 0,
  145.    4, 0, 7, 19,
  146. };
  147.  
  148. static const unsigned char nbit[4]={4,4,3,2};
  149.  
  150. ///////////////////////////////////////////////////////////////////////////
  151.  
  152. void AMPDecoder::L2_PrereadFrame() {
  153.     resetbits(frame_size);
  154. }
  155.  
  156. bool AMPDecoder::L2_DecodeFrame() {
  157.     int i, j;
  158.     int sb, ch;
  159.     int subbands, sidebands;
  160.     int alloctbl_idx;
  161.     int bitalloc[32][2];
  162.     int scfsi[32][2];
  163.     float scalefac[3][32][2];
  164.     float sample[3][2][32];
  165.  
  166.     // header's already been read... so fill the bit reservoir.
  167.  
  168.     resetbits(frame_size);
  169.  
  170.     // decode bit allocations for each subband.
  171.     //
  172.     // 1) Figure out which A-D table we want, based on mode, bitrate and sampling rate.
  173.  
  174.     if (is_mpeg2)
  175.         alloctbl_idx = 4;
  176.     else
  177.         alloctbl_idx = lookqt[mode][sr_index][br_index];
  178.  
  179.     // 2) Decode actual bit allocations.
  180.  
  181. //_RPT0(0,"Decoding bit allocations\n");
  182.  
  183.     if (mode == MODE_JOINTSTEREO)
  184.         sidebands = 4*(mode_ext+1);
  185.     else
  186.         sidebands = 32;
  187.  
  188.     sb=0;
  189.     for(i=0; i<4; i++)
  190.         for(j=0; j<look_nbat[alloctbl_idx][i]; j++) {
  191.  
  192.             if (sb < sidebands)
  193.                 for(ch=0; ch<channels; ch++)
  194.                     bitalloc[sb][ch] = look_bat[alloctbl_idx][i][getbits(nbit[i])];
  195.             else
  196.                 bitalloc[sb][0] = bitalloc[sb][1] = look_bat[alloctbl_idx][i][getbits(nbit[i])];
  197.  
  198.             ++sb;
  199.         }
  200.  
  201.     subbands = sb;
  202.     if (sidebands > subbands)
  203.         sidebands = subbands;
  204.  
  205.     // decode scale factor selector indices (scfsi)
  206.     //
  207.     // 2 bits per subband per channel, left-then-right if stereo
  208.  
  209.     for(sb=0; sb<subbands; sb++)
  210.         for(ch=0; ch<channels; ch++)
  211.             if (bitalloc[sb][ch])
  212.                 scfsi[sb][ch] = getbits(2);
  213.  
  214.     // decode scale factors
  215.     //
  216.     // 6, 12, or 18 bits per subband per channel, left-then-right if stereo
  217.  
  218.     for(sb=0; sb<subbands; sb++) {
  219.         for(ch=0; ch<channels; ch++) {
  220.             if (bitalloc[sb][ch]) {
  221.                 double c = c_f_tab[bitalloc[sb][ch]-1];
  222.  
  223.                 scalefac[0][sb][ch] = c*scalefactors[getbits(6)];
  224.  
  225.                 switch(scfsi[sb][ch]) {
  226.  
  227.                     case 0:        // three distinct scale factors
  228.                         scalefac[1][sb][ch] = c*scalefactors[getbits(6)];
  229.                         scalefac[2][sb][ch] = c*scalefactors[getbits(6)];
  230.                         break;
  231.  
  232.                     case 1:        // first two scale factors identical
  233.                         scalefac[1][sb][ch] = scalefac[0][sb][ch];
  234.                         scalefac[2][sb][ch] = c*scalefactors[getbits(6)];
  235.                         break;
  236.  
  237.                     case 3:        // last two scale factors identical
  238.                         scalefac[1][sb][ch] =
  239.                         scalefac[2][sb][ch] = c*scalefactors[getbits(6)];
  240.                         break;
  241.  
  242.                     case 2:        // all three scale factors identical
  243.                         scalefac[1][sb][ch] =
  244.                         scalefac[2][sb][ch] = scalefac[0][sb][ch];
  245.                         break;
  246.  
  247.                 }
  248.             } else
  249.                 sample[0][ch][sb] = sample[1][ch][sb] = sample[2][ch][sb] = 0;
  250.         }
  251.     }
  252.  
  253.     // decode samples (12 sets)
  254.  
  255.     static const unsigned char bitlookup[]={
  256.         0, 64, 65, 3, 66, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
  257.     };
  258.  
  259.     static const unsigned char group_bits[]={
  260.         5, 7, 10,
  261.     };
  262.  
  263.     for(sb=subbands; sb<32; sb++)
  264.         for(ch=0; ch<channels; ch++)
  265.             sample[0][ch][sb] = sample[1][ch][sb] = sample[2][ch][sb] = 0;
  266.  
  267.     for(i=0; i<12; i++) {
  268.         const float (*const sf_ptr)[2] = scalefac[i>>2];
  269.  
  270.  
  271.         for(sb=0; sb<sidebands; sb++) {
  272.             for(ch=0; ch<channels; ch++) {
  273.                 if (bitalloc[sb][ch]) {
  274.                     const unsigned char bits = bitlookup[bitalloc[sb][ch]];
  275.                     const double sf = sf_ptr[sb][ch];
  276.  
  277.                     if (bits<64) {
  278.                         int bias = (1<<(bits-1))-1;
  279.                         sample[0][ch][sb] = sf*(getbits(bits) - bias);
  280.                         sample[1][ch][sb] = sf*(getbits(bits) - bias);
  281.                         sample[2][ch][sb] = sf*(getbits(bits) - bias);
  282.                     } else {
  283.  
  284.                         // select between 3 grouping of 3, 5, or 9 levels each
  285.  
  286.                         const int v = getbits(group_bits[bits-64]);
  287.                         const signed char (*const grp_tbl)[3] = group_tbls[bits-64];
  288.  
  289.                         sample[0][ch][sb] = sf*grp_tbl[v][0];
  290.                         sample[1][ch][sb] = sf*grp_tbl[v][1];
  291.                         sample[2][ch][sb] = sf*grp_tbl[v][2];
  292.                     }
  293.                 }
  294.             }
  295.         }
  296.  
  297.         for(; sb<subbands; sb++) {
  298.             if (bitalloc[sb][0]) {
  299.                 const unsigned char bits = bitlookup[bitalloc[sb][0]];
  300.                 const double sf = sf_ptr[sb][0];
  301.  
  302.                 if (bits<64) {
  303.                     int bias = (1<<(bits-1))-1;
  304.                     sample[0][0][sb] = sf*(getbits(bits) - bias);
  305.                     sample[1][0][sb] = sf*(getbits(bits) - bias);
  306.                     sample[2][0][sb] = sf*(getbits(bits) - bias);
  307.                 } else {
  308.  
  309.                     // select between 3 grouping of 3, 5, or 9 levels each
  310.  
  311.                     const int v  = getbits(group_bits[bits-64]);
  312.                     const signed char (*grp_tbl)[3] = group_tbls[bits-64];
  313.  
  314.                     sample[0][0][sb] = sf*grp_tbl[v][0];
  315.                     sample[1][0][sb] = sf*grp_tbl[v][1];
  316.                     sample[2][0][sb] = sf*grp_tbl[v][2];
  317.                 }
  318.             }
  319.  
  320.             if (channels>1) {
  321.                 sample[0][1][sb] = sample[0][0][sb];
  322.                 sample[1][1][sb] = sample[1][0][sb];
  323.                 sample[2][1][sb] = sample[2][0][sb];
  324.             }
  325.         }
  326.  
  327.         // subband synthesis
  328.  
  329.         if (mode == MODE_MONO)
  330.             for(j=0; j<3; j++) {
  331.                 polyphase(&sample[j][0][0], NULL, psDest + 32*(i*3+j), false);
  332.             }
  333.         else
  334.             for(j=0; j<3; j++) {
  335.                 polyphase(&sample[j][0][0], &sample[j][1][0], psDest + 64*(i*3+j), false);
  336.             }
  337.     }
  338.  
  339.     // report proper sample count
  340.  
  341.     if (mode == MODE_MONO)
  342.         lSampleCount = 1152;
  343.     else
  344.         lSampleCount = 1152*2;
  345.  
  346.     return true;
  347. }
  348.